package com.xiaozhichao.io.netty;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.string.StringEncoder;

import java.util.Date;

public class NettyClient {

    public static void connect(){
        Bootstrap boot = new Bootstrap();
        NioEventLoopGroup group = new NioEventLoopGroup();
        boot.group(group)
                .channel(NioSocketChannel.class)
                .handler(new ChannelInitializer<Channel>() {
                    @Override
                    protected void initChannel(Channel s) throws Exception {
                        s.pipeline().addLast(new StringEncoder());
                    }
                });
        Channel channel = boot.connect("127.0.0.1",8000).channel();
        while(true){
            channel.writeAndFlush(new Date() + " helloWorld");
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {

            }
        }
    }

    public static void main(String[] args) {
        NettyClient.connect();
    }
}
